import Cookies from 'js-cookie'
import CONSTANTS from '@/config/constants'
import util from '@/libs/util'
import mixin from '../mixin'

const FORM_DATA = {
    realname: '',
    phone: '',
    sex: 1,
    age: 20,
    school: '',
    diplomasId: -1
}

export default {
    props: ['baseuser', 'themeId'],
    mixins: [mixin],
    data() {
        return {
            columns: [{
                type: 'selection',
                width: 60,
                align: 'center'
            }, {
                title: '用户名称',
                key: 'realname',
                width: 100,
                render: (h, params) => {
                    return h('div', {}, params.row.member.realname);
                }
            }, {
                title: '联系电话',
                key: 'phone',
                width: 100,
                render: (h, params) => {
                    return h('div', {}, params.row.member.phone);
                }
            }, {
                title: '性别',
                key: 'sex',
                width: 70,
                render: (h, params) => {
                    return h('div', {}, (params.row.member.sex === 1 ? '男' : '女'));
                }
            }, {
                title: '年龄',
                key: 'age',
                width: 70,
                render: (h, params) => {
                    return h('div', {}, params.row.member.age);
                }
            }, {
                title: '学校/所在单位',
                key: 'school',
                width: 120,
                render: (h, params) => {
                    return h('div', {}, params.row.member.school);
                }
            }, {
                title: '奖项',
                key: 'diplomas',
                width: 70,
                render: (h, params) => {
                    return h('div', {}, params.row.diplomas.name);
                }
            }, {
                title: '团员',
                key: 'team',
                render: (h, params) => {
                    return h('div', {}, params.row.team);
                }
            }/* , {
                title: '操作',
                key: 'action',
                align: 'center',
                render: (h, params) => {
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            on: {
                                click: () => {
                                    this.delete(params.row.id);
                                }
                            }
                        }, '删除')
                    ]);
                }
            } */],
            data: [],
            total: 0,
            pageIndex: 1,
            pageSize: 100,
            dipList: [],

            SERVER_HOST: CONSTANTS.SERVER_HOST,
            editModal: false,

            generateData: {
            },

            formData: Object.assign({}, FORM_DATA),
            ruleInline: {
                realname: [
                    { required: true, message: '会员名称不能为空！', trigger: 'blur' }
                ],
                phone: [
                    { required: true, message: '联系电话不能为空！', trigger: 'blur' }
                ],
                school: [
                    { required: true, message: '学校/所在单位不能为空！', trigger: 'blur' }
                ],
                diplomasId: [
                    { type: 'number', required: true, message: '奖项不能为空！', trigger: 'change' }
                ]
            },
            downloadUrl: CONSTANTS.LOCAL_HOST + '/static/file/memberImport.xlsx'
        }
    },
    mounted() {
        this.showLoading()
        this.loadDip()
        this.loadList()
        this.$Spin.hide()
    },
    methods: {
        async loadList() {
            this.showLoading()
            const res = await util.ajax.post(CONSTANTS.GET_MEMBER_BY_THEME, util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                orgid: this.baseuser.orgId,
                themeId: this.themeId,
                realName: '',
                phone: ''
            }))
            this.data = res.data.data.list
            this.total = res.data.data.total
            this.$Spin.hide()
        },
        async loadDip() {
            const res = await util.ajax.post(CONSTANTS.GET_DIPLOMA_BY_THEME, util.toFormData({
                themeId: this.themeId
            }))
            this.dipList = res.data.data
            this.dipList.unshift({ id: -1, name: '请选择' })
        },
        async save() {
            /* const req = Object.assign({}, this.generateData)
            if (this.baseuser.type === 2) {
                req.orgId = this.baseuser.orgId
            }
            const res = await util.ajax.post(CONSTANTS.SEAL_SAVE, util.toFormData(req))
            return res.data.message === 'SUCCESS' */
        },
        deleteByIds() {
            if (this.ids.length > 0) {
                this.$Modal.confirm({
                    title: '提示',
                    content: '<p>确定删除所选用户？</p>',
                    onOk: () => {
                        util.ajax.post('/busine/theme/member/deleteByIds', util.toFormData({
                            ids: this.ids
                        })).then(res => {
                            if (res.data.message === 'SUCCESS') {
                                this.$Message.success('删除成功！');
                                this.loadList();
                            }
                        });
                    }
                });
            } else {
                this.$Modal.error({
                    content: '请选择要删除用户！'
                });
            }
        },
        addMember() {
            this.formData = Object.assign({}, FORM_DATA)
            this.editModal = true
        },
        saveMember(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    let formData = {};
                    for (var key in this.formData) {
                        formData[key] = this.formData[key];
                    }
                    formData.themeId = this.themeId;
                    formData.orgid = this.baseuser.orgId;
                    util.ajax.post(CONSTANTS.MEMBER_SAVE, util.toFormData(formData)).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.$Message.success('会员添加成功！');
                            this.editModal = false;
                            this.loadList()
                        } else {
                            this.$Message.error(res.data.message);
                        }
                    });
                }
            });
        },
        closeModel(formName) {
            this.$refs[formName].resetFields();
            this.editModal = false;
        },
        uploadSuccess (res, file) {
            if (res.message === 'SUCCESS') {
                this.$Modal.success({
                    content: res.data,
                    onOk: () => {
                        this.loadList();
                    }
                });
            } else {
                this.$Modal.error({
                    content: res.message
                });
            }
        }
    },
    computed: {
    },
    beforeDestroy() {
    }
};